package ControlLayer;
import java.util.ArrayList;

import ModelLayer.*;

/**
 * Controller class for the address book
 * 
 * @author (GroupA) 
 * @version (2013.11.7)
 * 
 */
public class AddressCtr
{
   
    private static AddressBook addressBookCtr;
    
    //constructer for the AdressBook class; does nothing 
    public AddressCtr()
    {
        addressBookCtr = new AddressBook();
    }
    
    public static Person getPerson(long _id)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        return addressBookCtr.getPerson(_id);
    }
    
    
    public static void createPerson(long _id, String _name, String _address, String _postalCode, String _city, String _phone)
    {   
       //creates a Person object and stores it in the container
       //if it doesn't exist, it gives a message
       if (getPerson(_id) == null)
       {
           Person p = new Person();
           p.setPersonID(_id);
           p.setName(_name);
           p.setAddress(_address);
           p.setPostalCode(_postalCode);
           p.setCity(_city);
           p.setPhone(_phone);
           addressBookCtr.addPerson(p);
           System.out.println("Successfully Added!");
       }
       else
           System.out.println("This Person already exists!");
    }
    
    public static void deletePerson(long _id)
    {
        //search for the person and delete the object from the container
        //if it doesn't exist, it gives a message
        if (AddressCtr.getPerson(_id) != null)
        {
            addressBookCtr.deletePerson(_id);
            System.out.println("The person was deleted");
        }
        else
            System.out.println("The Person is not present in the address book");
    }
    
    public static void updatePerson(Person _p)
    {
        //search for a person by its ID 
        //deletes the information of the person except its ID 
            addressBookCtr.deletePerson(_p.getPersonID());
            addressBookCtr.addPerson(_p);        
    }
    
    public static ArrayList<Person> listAllPersons()
    {
          return addressBookCtr.getAddressBook();
    }
}
